package com.atguigu.springcloud.controller;


import com.atguigu.springcloud.entities.Payment;
import com.atguigu.springcloud.lb.LoadBalancer;
import com.atguigu.springcloud.result.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URI;
import java.util.List;

/**
 * @author yanglin
 * @create 2023-11-09 9:07 星期四
 * description:
 */
@RestController
@Slf4j
public class OrderController {
    
    @Resource
    private RestTemplate restTemplate;
    
    public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
    
    @Autowired
    private LoadBalancer loadBalancer;
    
    @Autowired
    private DiscoveryClient discoveryClient;
    
    @GetMapping("/consumer/payment/create")
    public CommonResult<Payment> create(Payment payment){
        //payment单机版
        return restTemplate.postForObject("http://localhost:8001" + "/payment/create",payment,CommonResult.class);
        
        //payment集群版
        
    } 
    
    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") Long id){
        return restTemplate.getForObject("http://localhost:8001" + "/payment/get/" + id,CommonResult.class);
    }

    //--------------------------------------------------------------------------------------
    
    /**
     * 测试 RestTemplate 的getForEntity 和 postForEntity
     */
    @GetMapping("/consumer/payment/test/create")
    public CommonResult testPostForEntity(Payment payment){
        ResponseEntity<CommonResult> commonResultResponseEntity = restTemplate.postForEntity("http://localhost:8001" + "/payment/create", payment, CommonResult.class);
        CommonResult commonResult = commonResultResponseEntity.getBody();
        //响应状态码
        HttpStatus statusCode = commonResultResponseEntity.getStatusCode();
        
        int statusCodeValue = commonResultResponseEntity.getStatusCodeValue();
        HttpHeaders headers = commonResultResponseEntity.getHeaders();
        log.info("statusCode = " + statusCode + "\t statusCodeValue = " + statusCodeValue + "\t headers = " + headers);
        if (commonResultResponseEntity.getStatusCode().is2xxSuccessful()){
            return commonResult;
        }else {
            return new CommonResult(444,"操作失败");
        }
        
    }
    
    @GetMapping("/consumer/payment/test/get/{id}")
    public CommonResult testGetForEntity(@PathVariable("id") Long id){
        ResponseEntity<CommonResult> forEntity = restTemplate.getForEntity("http:localhost:8001" + "/payment/get/" + id, CommonResult.class);
        CommonResult commonResult = forEntity.getBody();
        HttpStatus statusCode = forEntity.getStatusCode();
        int statusCodeValue = forEntity.getStatusCodeValue();
        HttpHeaders headers = forEntity.getHeaders();
        log.info("statusCode = " + statusCode + "\t statusCodeValue = " + statusCodeValue + "\t headers = " + headers);
        return commonResult;
    }
    
    @GetMapping("/consumer/payment/lb")
    public String getPaymentLB(){
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        if (instances == null || instances.size() == 0){
            return null;
        }
        ServiceInstance serviceInstance = loadBalancer.instances(instances);
        URI uri = serviceInstance.getUri();
        log.info("uri = {}",uri);
        return restTemplate.getForObject(uri + "/payment/lb",String.class);
    }
    
    //---zipkin-----
    @GetMapping("/consumer/payment/zipkin")
    public String orderZipkin(){
        String result = restTemplate.getForObject("http://localhost:8001" + "/payment/zipkin", String.class);
        return result;
    }
    
}
